#include "pch.h"
#include "FIni.h"

CFIni::CFIni()
{

}

CFIni::~CFIni()
{
}

CFIni::CFIni(char* filename)
{
	try
	{		
		GetExepath(m_szIniName);
		char pData[MAX_PATH] = { "CompareParm\\" };
		strcat_s(pData, filename);
		strcat_s(m_szIniName, pData);
	}
	catch (...)
	{
		throw("ERROR");
	}
}

void CFIni::GetExepath(char * pPath)
{
	try
	{
		char pExePath[MAX_PATH];
		GetModuleFileNameA(NULL, pExePath, MAX_PATH); //If this parameter is NULL, GetModuleFileName returns the path for the file containing the current process. 
		char* pEnd = strrchr(pExePath, '\\');  //Scan a string for the last occurrence of a character.	
		*(pEnd + 1) = NULL;
		if (pPath)
			strcpy_s(pPath, sizeof(pExePath), pExePath);
	}
	catch (...)
	{
		throw("ERROR");
	}
}


int CFIni::GetInteger(const char * szAppName, const char * szKeyName, int nDefault)
{
	char szTemp[MAX_PATH] = { 0 };
	char szDefault[MAX_PATH] = { 0 };
	sprintf_s(szDefault, "%d", nDefault);
	GetString(szAppName, szKeyName, szDefault, szTemp);
	return atoi(szTemp);
}
void CFIni::SetInteger(const char * szAppName, const char * szKeyName, int nSrc)
{
	char szTemp[MAX_PATH] = { 0 };
	sprintf_s(szTemp, "%d", nSrc);
	WriteString(szAppName, szKeyName, szTemp);
}


void CFIni::GetString(const char * szAppName, const char * szKeyName, const char * szDefault, char * szDst)
{
	GetPrivateProfileStringA(szAppName, szKeyName, szDefault, szDst, MAX_PATH, m_szIniName);
	WritePrivateProfileStringA(szAppName, szKeyName, szDst, m_szIniName);
}
void CFIni::WriteString(const char * szAppName, const char * szKeyName, const char * szWrite)
{
	WritePrivateProfileStringA(szAppName, szKeyName, szWrite, m_szIniName);
}

CString CFIni::GetIniString(const char* pSec, const char* pKey, double dbDefault)
{
	double dbre = 0;
	char szRtn[MAX_PATH];
	try
	{
		char szSrc[MAX_PATH];
		sprintf_s(szSrc, "%.3f", dbDefault);
		GetPrivateProfileStringA(pSec, pKey, szSrc, szRtn, 256, m_szIniName);
	}
	catch (...)
	{
		throw("ERROR");
	}

	return (CString)szRtn;
}
